pseudoPODs
Smart Class Members That Masquerade as POD Members
The preceding section showed an example of how to use pseudoPODs for integers. This section shows the details of using pseudoPODs for different data types, including longs, pointers, arrays, etc.
ppod(fred_c, int, name); // create the pseudoPOD
name.ep=this; // place in constructor()
// of class fred_c
int get_name () { // handle reads,
// e.g. i = fred.name;
// add extra code here if needed.
return (name_value);
}
int set_name(int in) { // handle writes,
// e.g. fred.name = 35;
// add extra code here if needed.
name_value = in;
return (name_value);
}
same as for integer, but for "int" substitute "long".
same as for integer, but for "int" substitute one of "unsigned int", "unsigned long", "short", "unsigned short", "char", "unsigned char", "bool", "float", "double" or "long double".
ppod() fails for 3 word data types, e.g. "unsigned long int" or "unsigned short int". Use instead their 2 word equivalents "unsigned long" or "unsigned short".
ppod_p(fred_c, int*, name); // create the pseudoPOD
name.ep=this; // place in constructor()
// of class fred_c
int *get_name () { // handle reads,
// e.g. i = fred.name;
// add extra code here if needed.
return (name_value);
}
int *set_name(int *in) { // handle writes,
// e.g. fred.name = 35;
// add extra code here if needed.
name_value = in;
return (name_value);
}
same as for integer, but for "int*" substitute one of "unsigned int*", "unsigned long*", "short*", "unsigned short*", "char*", "unsigned char*", "bool*", "float*", "double*" or "long double*".
The pseudoPOD pointers in this article are simple ones - to explore various ways of making pointers more capable and more complex, google "smart pointers" or "auto_pointer" on the web, and see Scott Meyers' books on Effective C++.
ppod_array(fred_c, int, name, podlen); // create the
// pseudoPOD, an array of [podlen] elements.
// The ppod_array() code creates
// const static int name_array_len = podlen;
// place in constructor() of class fred_c
for (int i=0; i<name_array_len; ++i) {
name[i].ep=this;
};
int get_name (int index) { // handle reads,
// e.g. i=fred.name[7];
if ((index < 0) || (index >= name_array_len)) {
cout << "ERROR: get_name() - array index ["
<< index << "] out of bounds\n";
std::cin.get(); exit(1); // wait
// for keystroke, then exit
}
// add extra code here if needed.
return (name_value[index]);
}
int set_name(int index, int in) { // handle writes,
// e.g. fred.name[i] = 35;
// check for array index out of bounds.
if ((index < 0) || (index >= name_array_len)) {
cout << "ERROR: set_name() - array index ["
<< index << "] out of bounds\n";
std::cin.get(); exit(1); // wait
// for keystroke, then exit
}
// add extra code here if needed.
name_value[index] = in;
return (name_value[index]);
}
same as for integer, but for "int" substitute one of "unsigned int", "unsigned long", "short", "unsigned short", "char", "unsigned char", "bool", "float", "double" or "long double".
ppod_array_p(fred_c, int*, name, podlen); // create
// pseudoPOD array
// place in constructor() of class fred_c
for (int i=0; i<name_array_len; ++i) {
name[i].ep=this;
};
int *get_name (int index) { // handle reads,
// e.g. ip = fred.name[7];
if ((index < 0) || (index >= name_array_len)) {
cout << "ERROR: *get_name() - array index ["
<< index << "] out of bounds\n";
std::cin.get(); exit(1); // wait
// for keystroke, then exit
}
// add extra code here if needed.
return (name_value[index]);
}
int *set_name(int index, int *in) { // handle
// writes, e.g. fred.name[i] = 35;
// check for array index out of bounds.
if ((index < 0) || (index >= name_array_len)) {
cout << "ERROR: *set_name() - array index ["
<< index << "] out of bounds\n";
std::cin.get(); exit(1); // wait
// for keystroke, then exit
}
// add extra code here if needed.
name_value[index] = in;
return (name_value[index]);
}
same as for integer, but for "int*" or "int *" substitute one of "unsigned int*", "unsigned long*", "short*", "unsigned short*", "char*", "unsigned char*", "bool*", "float*", "double*" or "long double*".
same as before, but add "_ro" or "_wo" to the ppod() call, so that:
ppod() |
becomes |
ppod_ro() or ppod_wo(), |
ppod_p() |
becomes |
ppod_p_ro() or ppod_p_wo(), |
ppod_array() |
becomes |
ppod_array_ro() or ppod_array_wo(), and |
ppod_array_p() |
becomes |
ppod_array_p_ro() or ppod_array_p_wo(). |